package org.mars.rover.navigation;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.mars.rover.navigation.data.Location;
import org.mars.rover.navigation.exception.NavigationException;
import org.mars.rover.navigation.utils.NavigationConstants;
import org.mars.rover.navigation.utils.Navigator;

public class Main {

	public static void main(String args[]) throws Exception {

		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(System.in));

		System.out.println("*****************************");
		System.out.println("*****************************");
		System.out.println("**** MARS ROVER ONLINE ******");
		System.out.println("*****************************");
		System.out.println("*****************************");

		System.out.println("Enter the upper-right co-ordinates of plateau");
		String upperCorner = bufferedReader.readLine();
		int topx = Integer.parseInt(upperCorner.split(" ")[0]);
		int topy = Integer.parseInt(upperCorner.split(" ")[1]);

		System.out.println("Enter current location of Rover");
		String currentLocationInput = bufferedReader.readLine();
		int currentX = Integer.parseInt(currentLocationInput.split(" ")[0]);
		int currentY = Integer.parseInt(currentLocationInput.split(" ")[1]);
		String orientation = currentLocationInput.split(" ")[2];
		Location currentLocation = null;
		Location initalLocation = new Location(currentX, currentY, orientation);
		
		Navigator navigator = new Navigator(topx, topy);

		System.out
				.println("Enter \"I\" for move instructions or \"Q\" for quit");

		String input = bufferedReader.readLine();
		while (!input.equalsIgnoreCase(NavigationConstants.QUIT)) {

			System.out.println("Enter move instructions....");
			String moveInput = bufferedReader.readLine();

			char[] inst = moveInput.toCharArray();

			System.out.println("Processing..........");
			
			currentLocation = new Location(initalLocation);
			
//			System.out.println(currentLocation);
			
			for (char c : inst) {
				String value = String.valueOf(c);
				if (NavigationConstants.LEFT.equalsIgnoreCase(value)
						|| NavigationConstants.RIGHT.equalsIgnoreCase(value)) {
					// Trun Rover and calculate direction
					// System.out.println("Turning");
					String oriString = navigator.turnRover(currentLocation
							.getOrientation(), value);
					currentLocation.setOrientation(oriString);
				} else if (NavigationConstants.MOVE.equalsIgnoreCase(value)) {
					// Move Rover
					// System.out.println("Moving");
					try {
						currentLocation = navigator.move(currentLocation,
								currentLocation.getOrientation());
					} catch (NavigationException e) {
						e.printStackTrace();
						System.out.println(currentLocation);
					}
				}
			}
			System.out.println("New Location is " + currentLocation.getX()
					+ " " + currentLocation.getY() + " "
					+ currentLocation.getOrientation());

			System.out
					.println("Enter \"I\" for move instructions or \"Q\" for quit");

			input = bufferedReader.readLine();
		}

	}

}
